//
//  SecondViewController.m
//  王宇先_09
//
//  Created by wyx on 16/3/23.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "SecondViewController.h"
#import "TableViewOfContact.h"
#import "ModleOfContact.h"
#pragma mark - 声明 协议
@interface SecondViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic, retain)NSArray *arrOFKeys;
@property (nonatomic, retain)NSMutableDictionary *dicOfNew;
@property (nonatomic, retain)NSMutableArray *arr1;
@end

@implementation SecondViewController

#pragma mark - dealloc
-(void)dealloc{
    [_dic release];
    [_tableView release];
    [_arrOFKeys release];
    [_dicOfNew release];
    [_arr1 release];
    [super dealloc];
}
#pragma mark - 加载
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"新闻";
    [self createTableView];
    [self handleJSON];
}
#pragma mark - Date
- (void)handleJSON{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"news" ofType:@"txt"];
    NSData *date = [NSData dataWithContentsOfFile:path];
    
    //核心 API
    id result = [NSJSONSerialization JSONObjectWithData:date options:NSJSONReadingMutableContainers error:nil];
    //NSLog(@"%@",result);
    NSMutableArray *arrJson = [result objectForKey:@"news"];
    self.arr1 = [NSMutableArray array];
    NSMutableArray *arrOfNew = [NSMutableArray array];
    for (NSDictionary *dic in arrJson) {
        ModleOfContact *modle = [[ModleOfContact alloc] init];
        //modle赋值(KVC)
        [modle setValuesForKeysWithDictionary:dic];
        //modle放入新数组中
        [_arr1 addObject:modle];
        [modle release];
    }
    //将modle 放入新字典中
    [self.dicOfNew setValue:arrOfNew forKey:@"title"];

    self.arrOFKeys = [[self.dicOfNew allKeys] sortedArrayUsingSelector:@selector(compare:)].mutableCopy;
}
#pragma mark - 创建 tableView
- (void)createTableView{
    self.tableView = [[TableViewOfContact alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    //指定代理人
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor redColor];
    //注册 cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
}
#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arr1.count;
    
}
#pragma mark - cell 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    ModleOfContact *modle = [self.arr1 objectAtIndex:indexPath.row];
    cell.textLabel.text = modle.title;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
